﻿using System;
using System.Collections.Generic;
using System.Text;

namespace LBSoft.Common.DotNetExt
{
    public static class ExtString
    {

        #region 转换为bool值
        /// <summary>
        /// 转换为bool值，当字符串不是一个合法的bool值的时候，返回false
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static Boolean ToBoolean(this String source)
        {
            return source.ToBoolean(false);
        }

        /// <summary>
        /// 转换为bool值，当字符串不是一个合法的bool值的时候，返回指定的默认值
        /// </summary>
        /// <param name="source"></param>
        /// <param name="defaultValue">转换失败时返回默认值</param>
        /// <returns></returns>
        public static Boolean ToBoolean(this String source, bool defaultValue)
        {
            if (string.IsNullOrEmpty(source))
                return defaultValue;

            source = source.Trim();
            bool value = false;
            if (bool.TryParse(source, out value))
            {
                return value;
            }

            return defaultValue;
        }
        #endregion

        #region 转换为int32
        /// <summary>
        /// 转换为int值，当字符串不是一个合法的Int值的时候，返回0
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static Int32 ToInt32(this String source)
        {
            return source.ToInt32(0);
        }

        /// <summary>
        /// 转换为int值，当字符串不是一个合法的Int值的时候，返回指定的默认值
        /// </summary>
        /// <param name="source"></param>
        /// <param name="defaultValue">转换失败时返回默认值</param>
        /// <returns></returns>
        public static Int32 ToInt32(this String source, int defaultValue)
        {
            if (string.IsNullOrEmpty(source))
                return defaultValue;

            source = source.Trim();
            int value = 0;
            if (int.TryParse(source, out value))
            {
                return value;
            }
            return defaultValue;
        }

        /// <summary>
        /// 将以,隔开的字符串转换为int[] 若转换失败返回null
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static int[] ToIntArray(this String source)
        {
            if (string.IsNullOrEmpty(source))
            {
                return null;
            }
            source = source.Trim();
            int[] result = null;
            try
            {
                result = Array.ConvertAll<string, int>(source.Split(','), Convert.ToInt32);
            }
            catch (System.Exception ex)
            {
                return null;
            }
            return result;
        }
        #endregion

        #region 转换为int64
        ///// <summary>
        ///// 转换为int值，当字符串不是一个合法的Int值的时候，返回0
        ///// </summary>
        ///// <param name="source"></param>
        ///// <returns></returns>
        //public static Int64 ToInt64(this String source)
        //{
        //    return source.ToInt64(0);
        //}

        ///// <summary>
        ///// 转换为int64值，当字符串不是一个合法的Int值的时候，返回指定的默认值
        ///// </summary>
        ///// <param name="source"></param>
        ///// <param name="defaultValue">转换失败时返回默认值</param>
        ///// <returns></returns>
        //public static Int64 ToInt64(this String source, Int64 defaultValue)
        //{
        //    if (string.IsNullOrEmpty(source))
        //        return defaultValue;

        //    source = source.Trim();
        //    Int64 value = 0;
        //    if (Int64.TryParse(source, out value))
        //    {
        //        return value;
        //    }
        //    return defaultValue;
        //}
        #endregion

        #region 转换为dateTime
        /// <summary>
        /// 转换为DateTime值，当字符串不是一个合法的DateTime值的时候，返回指定的默认值
        /// </summary>
        /// <param name="source"></param>
        /// <param name="defalultValue">转换失败时返回默认值</param>
        /// <returns></returns>
        public static DateTime ToDateTime(this String source, DateTime defaultValue)
        {
            if (string.IsNullOrEmpty(source))
                return defaultValue;

            source = source.Trim();

            DateTime value = default(DateTime);
            if (DateTime.TryParse(source, out value))
            {
                return value;
            }
            return defaultValue;
        }

        /// <summary>
        /// 转换为DateTime值，当字符串不是一个合法的DateTime值的时候，返回指定的默认值
        /// </summary>
        /// <param name="source"></param>
        /// <param name="defalultValue">转换失败时返回默认值</param>
        /// <returns></returns>
        public static DateTime ToDateTime(this String source, int defaultYear, int defaultMonth, int defaultDay)
        {
            DateTime d = new DateTime(defaultYear, defaultMonth, defaultDay);
            return source.ToDateTime(d);
        }
        #endregion

        #region 转换为Guid?
        /// <summary>
        /// 转换为Guid值，当字符串不是一个合法的Guid值的时候，返回指定的默认值
        /// </summary>
        /// <param name="source"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static Guid? ToGuid(this String source, Guid? defaultValue)
        {
            if (string.IsNullOrEmpty(source))
                return defaultValue;

            source = source.Trim();
            try
            {
                return new Guid(source);
            }
            catch (System.Exception ex)
            {
                return defaultValue;
            }
        }

        /// <summary>
        /// 转换为Guid值，当字符串不是一个合法的Guid值的时候，返回null
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static Guid? ToGuid(this string source)
        {
            return source.ToGuid(null);
        }

        /// <summary>
        /// 将以,隔开的字符串转换为Guid[] 若转换失败返回null
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static Guid[] ToGuidArray(this string source)
        {
            if (string.IsNullOrEmpty(source))
            {
                return null;
            }
            source = source.Trim();
            List<Guid> list = new List<Guid>();
            try
            {
                string[] strs = source.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string str in strs)
                {
                    Guid temp = new Guid(str);
                    list.Add(temp);
                }
            }
            catch (System.Exception ex)
            {
                return null;
            }
            return list.ToArray();
        }
        #endregion

        #region 获取英文字符长度(汉字算两个字符长度)
        /// <summary>
        /// 获取字符串英文长度(汉字算两个字符长度)
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static int GetEngLishLength(this String source)
        {
            if (string.IsNullOrEmpty(source))
                return 0;

            ASCIIEncoding ascii = new ASCIIEncoding();
            int len = 0;
            byte[] s = ascii.GetBytes(source);
            for (int i = 0; i < s.Length; i++)
            {
                if ((int)s[i] == 63)
                {
                    len += 2;
                }
                else
                {
                    len += 1;
                }
            }
            return len;
        }
        #endregion

        /// <summary>
        /// 当字符串小于指定长度的时候，为字符串补充空格，指定长度中文按2个长度计算
        /// </summary>
        /// <param name="source"></param>
        /// <param name="length">指定长度(中文按2个长度计算)</param>
        public static string AddSpace(this String source, int length)
        {
            int sourceLength = source.GetEngLishLength();
            if (length > sourceLength)
            {
                for (int i = sourceLength; i <= length; i++)
                {
                    source += " ";
                }
            }
            return source;
        }
    }

}
